package com.lx.sync.core.executor;

import com.google.common.collect.Lists;
import com.lx.sync.core.constant.ExtDataEsInxConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 外部数据同步全局配置
 *
 * @author machen
 * @date 2020/11/5 14:32
 */
@Component
public class ExtConfiguration {



    @Value("${sync.executor.batch.size:1000}")
    private int batchSize;

    private final static Map<String, Boolean> fastSyncConfig = new ConcurrentHashMap<>();
    private static final List<String> DEFAULT_FAST_LIST = Lists.newArrayList(
            ExtDataEsInxConstant.DRUG_ES_IDX_NAME,
            ExtDataEsInxConstant.MCS_PROD_IDX_NAME);

    @PostConstruct
    public void init() {
        DEFAULT_FAST_LIST.forEach(index -> fastSyncConfig.put(index, true));
    }

    public void updateFastSyncConfig(String indexName, boolean enabled) {
        fastSyncConfig.put(indexName, enabled);
    }

    public static SyncExecutor newSyncExecutor(Integer executorType,AbstractSyncService extSyncAbstract) {

        if (1 == executorType) {
            return new FastSyncExecutor(extSyncAbstract);
        }
        return new BaseSyncExecutor(extSyncAbstract);
    }

    public int getBatchSize() {
        return batchSize;
    }
}
